package com.solution.service;

import java.util.List;

import javax.ejb.EJBException;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;

import org.apache.log4j.Logger;
import org.jboss.annotation.ejb.RemoteBinding;

import com.solution.constant.SchedulerConstants;
import com.solution.dao.DAOFactory;
import com.solution.dao.SchdCronTriggersDAO;
import com.solution.dao.SchdJobAssociationsDAO;
import com.solution.dao.SchdJobDetailsDAO;
import com.solution.exception.DAOException;
import com.solution.exception.ServiceBeanException;
import com.solution.model.SchdJobAssociations;
import com.solution.util.SearchVO;

/**
 * This class exposes all the services for creation, update, read and deletion
 * of SchdJobAssociationsService.
 * 
 * 
 */

@Stateless
@Remote(SchdJobAssociationsService.class)
@RemoteBinding(jndiBinding = "SchdJobAssociationsService")
@TransactionManagement(TransactionManagementType.CONTAINER)
public class SchdJobAssociationsServiceImpl implements
		SchdJobAssociationsService {

	/**
	 * DAO instance.
	 */
	private static SchdJobDetailsDAO schdJobDetailsDAO = (SchdJobDetailsDAO) DAOFactory
			.getDAOInstance(SchdJobDetailsDAO.class.getName());

	private static SchdCronTriggersDAO schdCronTriggersDAO = (SchdCronTriggersDAO) DAOFactory
			.getDAOInstance(SchdCronTriggersDAO.class.getName());

	private static SchdJobAssociationsDAO schdJobAssociationsDAO = (SchdJobAssociationsDAO) DAOFactory
			.getDAOInstance(SchdJobAssociationsDAO.class.getName());

	/**
	 * Logger instance.
	 */
	private Logger log = Logger.getLogger(SchdJobAssociationsServiceImpl.class
			.getName());

	public SchdJobAssociations createFireNowJobAssociation(Long ownerId,Long ownerType,Long schemeId,String action)
			throws ServiceBeanException {
		try {
			SchdJobAssociations schdJobAssociations = new SchdJobAssociations();
			schdJobAssociations
					.setDescription("Fire now for Scheme Scheduling");
			schdJobAssociations.setIsActive(false);
			if(ownerId != null){
				schdJobAssociations.setParameters(ownerId+":"+ownerType+":"+schemeId+":"+action);
			}else{
				schdJobAssociations.setParameters(null);
			}
			schdJobAssociations.setSchdCronTriggers(schdCronTriggersDAO
					.read(SchedulerConstants.TRIGGER_NOW_ID));
			schdJobAssociations.setSchdJobDetails(schdJobDetailsDAO
					.read(SchedulerConstants.SCHEDULE_SCHEME_JOB_ID));

			return schdJobAssociationsDAO.create(schdJobAssociations);
		} catch (DAOException de) {
			throw new EJBException(de);
		}
	}

	public List<SchdJobAssociations> getActiveJobAssosciations()
			throws ServiceBeanException {
		try {
			SearchVO searchVO = new SearchVO();
			searchVO.addEqualsCondition("isActive", true);
			return schdJobAssociationsDAO.search(searchVO);
		} catch (DAOException de) {
			throw new EJBException(de);
		}
	}

	public String getJobAssociationParameter(String jobName, String jobGroup,
			String triggerName, String triggerGroup)
			throws ServiceBeanException {
		log
				.debug("Entering :: SchdJobAssociationsServiceImpl:getJobAssociationParameter()");
		try {
			log
					.debug("Exiting :: LocationManagementServiceImpl:getHierarchyListByTypeORParent()");
			return schdJobAssociationsDAO.getJobAssociationParameter(jobName,
					jobGroup, triggerName, triggerGroup);
		} catch (DAOException ex) {
			log.error(ex);
			throw new EJBException(ex);
		}
	}
	
	public String getJobAssociationParameter(String jobName, String jobGroup)
			throws ServiceBeanException {
		log
				.debug("Entering :: SchdJobAssociationsServiceImpl:getJobAssociationParameter()");
		try {
			log
					.debug("Exiting :: LocationManagementServiceImpl:getHierarchyListByTypeORParent()");
			return schdJobAssociationsDAO.getJobAssociationParameter(jobName,
					jobGroup);
		} catch (DAOException ex) {
			log.error(ex);
			throw new EJBException(ex);
		}
	}

}
